package hellfire.platform.graphics;

import java.util.Vector;

import android.util.Log;

import hellfire.game.Algorithms;
import hellfire.game.ChickenDebris;
import hellfire.system.graphics.GLColor;
import hellfire.system.graphics.GLGPUCanvas;
import hellfire.system.graphics.GLParticleEffect;
import hellfire.system.graphics.GLRelVec2;
import hellfire.system.graphics.GLVec2;
import hellfire.system.graphics.GLParticleEffect.Particle;

public class ChickenDebrisRenderer extends AbstractRenderer
{
	private GLParticleEffect effect;
	private GLGPUCanvas canvas;
	
	private Vector<Particle> particles;
	
	private int frames = 0;
	
	public ChickenDebrisRenderer(ChickenDebris d)
	{
		particles = new Vector<Particle>();
		
		canvas = new GLGPUCanvas(new GLVec2(128, 128), d.getRenderPos(), d.getRenderDim());
		GLVec2 vel = d.getChicken().getVelocity().normalize();
        
		double r1 = Math.random(), r2 = Math.random();
		
		for(int i = 0; i < 3 + (int)(r1*3); i++)
        {
            GLVec2 deflected = Algorithms.vectorDeflection(vel, vel.length()*0.5);
            deflected = deflected.mul(0.09f);
            
            Particle p = new Particle(deflected, 15, new GLColor(0.9f, 0, 0, 1));
            particles.add(p);
        }
		for(int i = 0; i < (int)(r2*2); i++)
        {
            GLVec2 deflected = Algorithms.vectorDeflection(vel, vel.length()*0.5);
            deflected = deflected.mul(-0.07f);
            
            Particle p = new Particle(deflected, 15, new GLColor(0.9f, 0, 0, 1));
            particles.add(p);
        }
        effect = new GLParticleEffect(new GLRelVec2(0,0), new GLVec2(1,1), particles);
        
	}
	
	public void gameFrame()
	{
		advanceSequence();
	}
	
	private void advanceSequence()
	{
		for(Particle p : particles)
		{
			p.size *= 0.90;
		}
		effect.updateSizes(particles);
		frames++;
	}
	
	public void draw(float[] matrix, Object obj)
	{
		canvas.draw(matrix);
		
		if(frames > 120) 
			return;
		if(canvas.isFBOInitialized())
		{
			canvas.startRenderTarget();
			effect.draw(matrix);
			canvas.stopRenderTarget();
		}
		//Log.d("R", "here");
		
	}
	
}
